package com.cn.hangyun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cn.hangyun.common.ExcelUtil;
import com.cn.hangyun.model.Ais;
import com.cn.hangyun.service.IAisService;


@Controller
@RequestMapping("/ais")
public class AisController {

	private IAisService aisService;

	@Autowired
	public void setAisService(IAisService aisService) {
		this.aisService = aisService;
	}

	@RequestMapping("/queryPage")
	@ResponseBody
	public void showData(Ais rec, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		JSONObject reJson = new JSONObject();
		List<Ais> listDet = aisService.getPageData(rec);
		long total=aisService.getRecordTotal(rec);
		Map mTotal = new HashMap();
		mTotal.put("total", total);
		mTotal.put("rows", listDet);
		reJson.putAll(mTotal);
		out.println(reJson);
	}
	@RequestMapping("/download")
	public String download(Ais rec,HttpServletResponse response) throws IOException {
		
		List<Ais> listRec = aisService.download(rec);
		
		String titleColumn[] = {"uid","mmsi","identityMessage","navigationalStatus",
				                "rateTurn","speedGround","positionAccuracy","east",
				                "north","utcSeconds","courseOverGround","insertDate"};  
        String titleName[] = {"检测ID","AIS编码(MMSI)","信息识别","航行状态",
        					  "转速","对地航速","位置精度","经度",
        					  "纬度","UTC","对地航向","数据登录时间"};  
        int titleSize[] = {15,20,15,15,15,15,15,15,15,15,15,20}; 
        response.setHeader("Content-Disposition", "attachment;filename="+"AIS历史记录");
	    ExcelUtil eu = new ExcelUtil(response,"AIS历史记录","AIS历史记录");
	    eu.wirteExcel(titleColumn, titleName, titleSize, listRec);
	    return null;
	}
}
